import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class LoginCollect extends GetxController {
  TextEditingController mPhoneCollect = TextEditingController();
  TextEditingController mCodeCollect = TextEditingController();


  RxBool isCheck = false.obs;
  Timer? _timer;
  int maxTime = 60;

  var codeStr = "获取验证码".obs;

  void setCheck(bool check) {
    print("---$check");
    isCheck(check);
  }

  void sendMessage(String phone) {
    if (maxTime > 0 && maxTime < 60) {
      return;
    }
    if (!GetUtils.isPhoneNumber(phone)) {
      Get.snackbar("提示", "请输入正确的手机号码");
      return;
    }
    if (maxTime == 0) {
      maxTime = 60;
    }
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (maxTime == 0) {
        codeStr("重新获取");
        _timer?.cancel();
      } else {
        maxTime = maxTime - 1;
        codeStr("($maxTime秒)");
      }
    });
  }

  @override
  void onClose() {
    _timer?.cancel();
    mPhoneCollect.dispose();
    mCodeCollect.dispose();
    super.onClose();
  }

  void login(String phone, String code) {
    if (!GetUtils.isPhoneNumber(phone)) {
      Get.snackbar("提示", "请输入正确的手机号码");
      return;
    }
    if (GetUtils.isNullOrBlank(code)!) {
      Get.snackbar("提示", "请输入验证码");
      return;
    }
    Get.snackbar("提示", "登录成功");
  }
}
